package com.chinawlx.wlxteacher.ui.fragment.calendar;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.WLXRxBus;
import com.chinawlx.wlxteacher.ui.event.ShowClassEvent;
import com.chinawlx.wlxteacher.utils.WLXCalendarUtil;
import com.chinawlx.wlxteacher.utils.WLXConstant;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者:吴名
 * 时间:16/9/3
 * 描述:日历-周模式下的Fragment
 */
public class WLXWeekFragment extends Fragment {

    @BindView(R.id.tv_yearMonthWeek)
    TextView mTvYearMonthWeek;
    @BindView(R.id.tv_sunday)
    TextView mTvSunday;
    @BindView(R.id.tv_classCountSunday)
    TextView mTvClassCountSunday;
    @BindView(R.id.tv_monday)
    TextView mTvMonday;
    @BindView(R.id.tv_classCountMonday)
    TextView mTvClassCountMonday;
    @BindView(R.id.tv_Tuesday)
    TextView mTvTuesday;
    @BindView(R.id.tv_classCountTuesday)
    TextView mTvClassCountTuesday;
    @BindView(R.id.tv_Wednesday)
    TextView mTvWednesday;
    @BindView(R.id.tv_classCountWednesday)
    TextView mTvClassCountWednesday;
    @BindView(R.id.tv_Thursday)
    TextView mTvThursday;
    @BindView(R.id.tv_classCountThursday)
    TextView mTvClassCountThursday;
    @BindView(R.id.tv_Friday)
    TextView mTvFriday;
    @BindView(R.id.tv_classCountFriday)
    TextView mTvClassCountFriday;
    @BindView(R.id.tv_Saturday)
    TextView mTvSaturday;
    @BindView(R.id.tv_classCountSaturday)
    TextView mTvClassCountSaturday;


    private int mYear;
    private int mMonth;
    private int mNumOfWeek;
    private Calendar mCalendar = Calendar.getInstance();
    private Calendar mCalendarToCompute = Calendar.getInstance();//用于去computeDate
    /**
     * 当月1号是周几
     */
    private int mDayOfWeek;

    private final ShowClassEvent mShowClassEvent = new ShowClassEvent();
    public static View mClickTvDay;//上一次点击的View(默认是今天)
    public static View mTodayView;//保留今天的View对象,便于改变背景

    private boolean isUpdate = false;//如果是更新Fragment,只更新单元格的课程状态,点击状态不更新


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
    Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mYear = bundle.getInt("year");
            mMonth = bundle.getInt("month");
            mNumOfWeek = bundle.getInt("numOfWeek");
        }
        View view = inflater.inflate(R.layout.item_calendar_week, null);
        ButterKnife.bind(this, view);

        mTvYearMonthWeek.setText(mYear + "年" + mMonth + "月 第" + mNumOfWeek + "周");
        mCalendar.set(mYear, mMonth - 1, 1);
        mDayOfWeek = mCalendar.get(Calendar.DAY_OF_WEEK);


        mTvSunday.setText(computeDay(0, mTvSunday, mTvClassCountSunday));
        mTvMonday.setText(computeDay(1, mTvMonday, mTvClassCountMonday));
        mTvTuesday.setText(computeDay(2, mTvTuesday, mTvClassCountTuesday));
        mTvWednesday.setText(computeDay(3, mTvWednesday, mTvClassCountWednesday));
        mTvThursday.setText(computeDay(4, mTvThursday, mTvClassCountThursday));
        mTvFriday.setText(computeDay(5, mTvFriday, mTvClassCountFriday));
        mTvSaturday.setText(computeDay(6, mTvSaturday, mTvClassCountSaturday));

        return view;
    }

    public static WLXWeekFragment newInstance(int year, int month, int numOfWeek) {
        WLXWeekFragment weekFragment = new WLXWeekFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("year", year);
        bundle.putInt("month", month);
        bundle.putInt("numOfWeek", numOfWeek);
        weekFragment.setArguments(bundle);
        return weekFragment;
    }

    @OnClick({R.id.tv_sunday, R.id.tv_monday, R.id.tv_Tuesday, R.id.tv_Wednesday, R.id
            .tv_Thursday, R.id.tv_Friday, R.id.tv_Saturday})
    public void onClick(View view) {
        if (mClickTvDay != null && mTodayView != null && mClickTvDay == mTodayView) {
            //上个点击的单元格是当天,就改为黄色圆圈
            //noinspection deprecation
            mClickTvDay.setBackgroundResource(R.drawable.shape_yellow_ring);
        } else {
            //不是当天,清除背景
            //noinspection deprecation
            mClickTvDay.setBackgroundDrawable(null);
        }
        switch (view.getId()) {
            case R.id.tv_sunday:
                mClickTvDay = mTvSunday;
                //读取当天的课程信息,ListView显示课程列表
                mCalendarToCompute.set(mYear, mMonth - 1, computeDate(0));
                WLXConstant.date = mCalendarToCompute.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
                break;
            case R.id.tv_monday:
                mClickTvDay = mTvMonday;
                mCalendarToCompute.set(mYear, mMonth - 1, computeDate(1));
                WLXConstant.date = mCalendarToCompute.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
                break;
            case R.id.tv_Tuesday:
                mClickTvDay = mTvTuesday;
                mCalendarToCompute.set(mYear, mMonth - 1, computeDate(2));
                WLXConstant.date = mCalendarToCompute.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
                break;
            case R.id.tv_Wednesday:
                mClickTvDay = mTvWednesday;
                mCalendarToCompute.set(mYear, mMonth - 1, computeDate(3));
                WLXConstant.date = mCalendarToCompute.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
                break;
            case R.id.tv_Thursday:
                mClickTvDay = mTvThursday;
                mCalendarToCompute.set(mYear, mMonth - 1, computeDate(4));
                WLXConstant.date = mCalendarToCompute.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
                break;
            case R.id.tv_Friday:
                mClickTvDay = mTvFriday;
                mCalendarToCompute.set(mYear, mMonth - 1, computeDate(5));
                WLXConstant.date = mCalendarToCompute.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
                break;
            case R.id.tv_Saturday:
                mClickTvDay = mTvSaturday;
                mCalendarToCompute.set(mYear, mMonth - 1, computeDate(6));
                WLXConstant.date = mCalendarToCompute.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
                break;
        }
        WLXRxBus.getInstance().send(mShowClassEvent);

        if (mTodayView != null && mClickTvDay == mTodayView) {//点击的是今天
            mClickTvDay.setBackgroundResource(R.drawable.shape_yellow_circle);
        } else {
            mClickTvDay.setBackgroundResource(R.drawable.shape_blue_circle);
        }
    }

    private int computeDate(int day) {
        int differ = 7 * mNumOfWeek - mDayOfWeek + day - 6;
        mCalendar.add(Calendar.DAY_OF_MONTH, differ);
        int dayOfMonth = mCalendar.get(Calendar.DAY_OF_MONTH);
        mCalendar.add(Calendar.DAY_OF_MONTH, -differ);//还原日期到某月1号
        return dayOfMonth;
    }

    /**
     * 算出这一周的周几的日期
     *
     * @param day          周几
     * @param tvDay        显示日期的单元格
     * @param tvClassCount 显示课程数量
     * @return
     */
    private String computeDay(int day, TextView tvDay, TextView tvClassCount) {
        int differ = 7 * mNumOfWeek - mDayOfWeek + day - 6;
        //===================begin=====因为算出来的日期得和初始值对比差值,所以用完之后得还原=======================
        mCalendar.add(Calendar.DAY_OF_MONTH, differ);
        int dayOfMonth = mCalendar.get(Calendar.DAY_OF_MONTH);

        if (!isUpdate){
            if (mCalendar.get(Calendar.MONTH) != mMonth - 1) {
                tvDay.setTextColor(getResources().getColor(R.color.gray_item));
                tvDay.setClickable(false);
            } else {
                tvDay.setClickable(true);
                tvDay.setTextColor(getResources().getColor(R.color.login_text_black));
                if (WLXCalendarUtil.isSameDay(mCalendar, WLXConstant.TODAY_CALENDAR)) {
                    //如果这一天等于今天,背景为黄色
                    tvDay.setBackgroundResource(R.drawable.shape_yellow_circle);
                    mTodayView = tvDay;
                    mClickTvDay = tvDay;
                }
            }
        }
        //读取当天的课程信息,显示课程数量
        long key = mCalendar.getTimeInMillis() / (60 * 60 * 24 * 1000);//当天日期换算成天数
        if (WLXConstant.mSchedulehashMap.containsKey(key)) {
            //如果有key,说明这一天有课
            tvClassCount.setText(WLXConstant.mSchedulehashMap.get(key).size() + "");
            tvClassCount.setVisibility(View.VISIBLE);
        } else {
            //没课
            tvClassCount.setVisibility(View.GONE);
        }

        mCalendar.add(Calendar.DAY_OF_MONTH, -differ);//还原日期到某月1号
        //===================end===因为算出来的日期得和初始值对比差值,所以用完之后得还原=======================
        if (dayOfMonth < 10) {
            return "0" + dayOfMonth + "";
        } else {
            return dayOfMonth + "";
        }
    }

    public void update() {
        isUpdate = true;
        mTvSunday.setText(computeDay(0, mTvSunday, mTvClassCountSunday));
        mTvMonday.setText(computeDay(1, mTvMonday, mTvClassCountMonday));
        mTvTuesday.setText(computeDay(2, mTvTuesday, mTvClassCountTuesday));
        mTvWednesday.setText(computeDay(3, mTvWednesday, mTvClassCountWednesday));
        mTvThursday.setText(computeDay(4, mTvThursday, mTvClassCountThursday));
        mTvFriday.setText(computeDay(5, mTvFriday, mTvClassCountFriday));
        mTvSaturday.setText(computeDay(6, mTvSaturday, mTvClassCountSaturday));
        isUpdate = false;
    }
}
